package com.by.controller;

import cn.hutool.core.bean.BeanUtil;
import com.by.bean.Customer;
import com.by.common.bean.PageBean;
import com.by.common.bean.ResultBean;
import com.by.common.controller.BaseController;
import com.by.query.CustomerQuery;
import com.by.service.CustomerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api")
@Api(tags = "客户管理")
@CrossOrigin
public class CustomerController extends BaseController {

    @Autowired
    CustomerService customerService;
    @ApiOperation("查询客户")
    @GetMapping("/customer")
    public ResultBean get(String id) {
        Map map = (Map) customerService.get(id);
        return success("客户信息", map);
    }
    @ApiOperation("查询用户列表")
    @PostMapping("/customer/list")
    public ResultBean<PageBean<List<Customer>>> queryByMap(@RequestBody CustomerQuery query) {
        PageBean bean = customerService.queryByMap(BeanUtil.beanToMap(query));
        return success("客户列表", bean);
    }

    @ApiImplicitParam(name = "id", value = "客户id", dataTypeClass = Integer.class, paramType = "query", required = true)
    @ApiOperation("删除客户")
    @DeleteMapping("/customer")
    public ResultBean delete(@RequestParam(required = true) int id) {
        int rs = customerService.delete(id);
        return deleteResult(rs);
    }

    @ApiOperation("修改客户")
    @PutMapping("/customer")
    public ResultBean update(@RequestBody Customer customer){
        int rs=customerService.update(customer);
        return updateResult(rs);
    }
    @ApiOperation("激活用户")
    @PutMapping("/customer/active")
    public ResultBean active(int id){
        int rs=customerService.active(id);
        return activeResult(rs);
    }
    @ApiOperation("下机用户")
    @PutMapping("/customer/deactivate")
    public ResultBean deactivate(int id){
        int rs=customerService.deactivate(id);
        return deactivateResult(rs);
    }
}
